/**
 * 
 */
package weibo4j.crawler;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import weibo4j.Emotion;
import weibo4j.Weibo;
import weibo4j.WeiboException;

/**
 * @author longrui
 *
 */
public class EmotionCrawler {
	private final static String typeSet[] = {"face", "ani", "cartoon"};
	private final static String langSet[] = {"cnname", "twname"};
	private Weibo weibo;
	
	public EmotionCrawler(Weibo weibo){
		this.weibo = weibo;
	}
	
	public List<String> getAllEmotionPhrase() throws WeiboException, InterruptedException{
		List<String> result = new ArrayList<String>();
		for(int i = 0; i < typeSet.length; i++){
			for(int j = 0; j < langSet.length; j++){
				List<Emotion> emotions = weibo.getEmotions(typeSet[i], langSet[j]);
				Thread.sleep(1000);		
				for(Emotion emotion : emotions){
					result.add(emotion.getPhrase());
				}
			}
		}
		return result;
	}
	
	/**
	 * @param args
	 * @throws WeiboException 
	 * @throws InterruptedException 
	 * @throws IOException 
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws WeiboException, InterruptedException, UnsupportedEncodingException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Clawing...");
		if (args.length < 2) {
			System.out
					.println("You need to specify WeiboID/Password combination to show Emotions.");
			System.out
					.println("Usage: java weibo4j.crawler.EmotionCrawler ID Password");
			System.exit(0);
		}
		
		Weibo weibo = new Weibo(args[0], args[1]);
		EmotionCrawler ec = new EmotionCrawler(weibo);
		List<String> ret = ec.getAllEmotionPhrase();
		
		//save to file;
		OutputStream os = new FileOutputStream("D:\\workspace\\KeyTerms\\exp_data\\emotioins.dat");
		int i = 0;
		for(String str : ret){
			if(i != 0){
				os.write("\n".getBytes("UTF-8"));
			}
			os.write(str.getBytes("UTF-8"));			
			i++;
		}
		os.close();		
		System.out.println(ret.size());
	}

}
